package com.minggo.charmword.net;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import com.minggo.charmword.cache.CacheUtils;
import com.minggo.charmword.model.Result;
import com.minggo.charmword.service.CharmWordApplication;
import com.minggo.charmword.util.AppContext;
import com.minggo.charmword.util.EncryptUtils;
import com.minggo.charmword.util.StringUtils;

/**
 * 拼装参数工具
 * 
 * @author minggo
 * @date 2014-9-15 上午11:11:22
 */
public class ApiUtil {

	/**
	 * 获取服务器返回结果 提交方式为GET
	 * 
	 * @param url
	 *            接口地址
	 * @param key
	 *            缓存key
	 * @param params
	 *            入参
	 * @param hasCacheInsteadof
	 *            是否无网络时用本地缓存
	 * @return
	 */
	public static Result getResult(String url, Map<String, Object> params, boolean hasCacheInsteadof, String key) {
		Result result = null;
		if (!AppContext.isNetworkConnected(CharmWordApplication.getInstance())) {
			if (hasCacheInsteadof && !StringUtils.isEmpty(key)) {
				String cachecontent = CacheUtils.getInstance().getDiskCache(key);
				if (!StringUtils.isEmpty(cachecontent)) {
					result = new Result();
					result.content = cachecontent;
					return result;
				}
			} else {
				return null;
			}
		}
		if (!AppContext.isNetworkConnected(CharmWordApplication.getInstance())) {
			return null;
		}
		try {
			result = ApiClient.http_get(url, params, true, false);
			params = null;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		if (result == null) {
			return null;
		}

		if (!result.success || result.content == null || result.content.equals("")) {
			return null;
		}

		return result;
	}

	/**
	 * 获取服务器返回结果 提交方式为GET
	 * 
	 * @param url
	 *            接口地址
	 * @param key
	 *            缓存key
	 * @param params
	 *            入参
	 * @param keepChangeLine
	 *            保持换行符号
	 * @param hasCacheInsteadof
	 *            是否无网络时用本地缓存
	 * @return
	 */
	public static Result getResult(String url, Map<String, Object> params, boolean hasCacheInsteadof, String key,
			boolean keepChangeLine) {
		Result result = null;
		if (!AppContext.isNetworkConnected(CharmWordApplication.getInstance())) {
			if (hasCacheInsteadof && !StringUtils.isEmpty(key)) {
				String cachecontent = CacheUtils.getInstance().getDiskCache(key);
				if (!StringUtils.isEmpty(cachecontent)) {
					result = new Result();
					result.content = cachecontent;
					return result;
				}
			} else {
				return null;
			}
		}
		if (!AppContext.isNetworkConnected(CharmWordApplication.getInstance())) {
			return null;
		}
		try {
			result = ApiClient.http_get(url, params, true, true, keepChangeLine);
			params = null;
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (result == null) {
			return null;
		}

		if (!result.success || result.content == null || result.content.equals("")) {
			return null;
		}

		return result;
	}

	/**
	 * 获取服务器返回结果 提交方式为GET
	 * 
	 * @param url
	 *            接口地址
	 * @param key
	 *            缓存key
	 * @param params
	 *            入参
	 * @param hasCacheInsteadof
	 *            是否无网络时用本地缓存
	 * @return
	 */
	public static Result getResult2(String url, Map<String, Object> params, boolean hasCacheInsteadof, String key) {
		Result result = null;
		if (!AppContext.isNetworkConnected(CharmWordApplication.getInstance())) {
			if (hasCacheInsteadof && !StringUtils.isEmpty(key)) {
				String cachecontent = CacheUtils.getInstance().getDiskCache(key);
				if (!StringUtils.isEmpty(cachecontent)) {
					result = new Result();
					result.content = cachecontent;
					return result;
				}
			} else {
				return null;
			}
		}
		if (!AppContext.isNetworkConnected(CharmWordApplication.getInstance())) {
			return null;
		}
		try {
			result = ApiClient.http_get(url, params, true, false);
			params = null;
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (result == null) {
			return null;
		}

		return result;
	}

	/**
	 * 获取服务器返回结果 提交方式为POST
	 * 
	 * @param url
	 *            接口地址
	 * @param key
	 *            缓存key
	 * @param params
	 *            入参
	 * @param hasCacheInsteadof
	 *            是否无网络时用本地缓存
	 * @return
	 */
	public static Result getResultByPost(String url, Map<String, Object> params, Map<String, File> files,
			boolean hasCacheInsteadof, String key) {
		Result result = null;
		if (!AppContext.isNetworkConnected(CharmWordApplication.getInstance())) {
			if (hasCacheInsteadof && !StringUtils.isEmpty(key)) {
				String cachecontent = CacheUtils.getInstance().getDiskCache(key);
				if (!StringUtils.isEmpty(cachecontent)) {
					result = new Result();
					result.content = cachecontent;
					return result;
				}
			} else {
				return null;
			}
		}
		
		try {
			result = ApiClient._post(url, params, files, false);
			params = null;
			files = null;
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (result == null) {
			return null;
		}

	/*	if (!result.success || result.content == null || result.content.equals("")) {
			return null;
		}*/

		// 添加SD卡缓存开始
		/*
		 * try { CacheUtils.getInstance().setDiskCache(key, result.content); }
		 * catch (IOException e) { e.printStackTrace(); }
		 */
		return result;
	}

	/**
	 * 添加必传参数 用于提交方式为GET的接口 mid 唯一识别码 pid 渠道号 imei 手机imei码 m_ver 手机型号 s_ver
	 * 系统版本号 versionName 包的版本名称 versionCode 包的版本号 pass 参数验证码
	 * md5(mid+pid+imei+m_ver+"minggo")
	 * 
	 * @return params
	 */
	public static Map<String, Object> addRequiredParam() {
		Map<String, Object> params = new HashMap<String, Object>();
		AppContext appContext = AppContext.getInstance();
		int versionCode = appContext.getPackageInfo().versionCode;
		String mid = appContext.GetAndroidId(0);
		String imei = appContext.getAndroidIMEI();
		String m_ver = appContext.GetMobileVersion();
		String s_ver = appContext.GetAndroidVersion();
		String versionName = appContext.getPackageInfo().versionName;
		try {

			String md5pass = new EncryptUtils().getMD5Str("minggo" + imei + "charmword");
			params.put(ApiUrl.pass, md5pass);
			params.put(ApiUrl.mid, mid);
			params.put(ApiUrl.imei, imei);
			params.put(ApiUrl.versionCode, versionCode);
			params.put(ApiUrl.versionName, versionName);

			params.put(ApiUrl.m_ver, URLEncoder.encode(m_ver, "utf-8"));
			params.put(ApiUrl.s_ver, URLEncoder.encode(s_ver, "utf-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return params;
	}

	/**
	 * 添加必传参数 用于提交方式为POST的接口 mid 唯一识别码 pid 渠道号 imei 手机imei码 m_ver 手机型号 s_ver
	 * 系统版本号 versionName 包的版本名称 versionCode 包的版本号 pass 参数验证码
	 * md5(mid+pid+imei+m_ver+"minggo")
	 * 
	 * @return params
	 */
	public static Map<String, Object> addRequiredParamByPost() {
		Map<String, Object> params = new HashMap<String, Object>();
		AppContext appContext = AppContext.getInstance();
		int versionCode = appContext.getPackageInfo().versionCode;
		String mid = appContext.GetAndroidId(0);
		String imei = appContext.getAndroidIMEI();
		String m_ver = appContext.GetMobileVersion();
		String s_ver = appContext.GetAndroidVersion();
		String versionName = appContext.getPackageInfo().versionName;

		params.put(ApiUrl.mid, mid);
		params.put(ApiUrl.imei, imei);
		params.put(ApiUrl.versionCode, versionCode);
		params.put(ApiUrl.versionName, versionName);
		try {

			String md5pass = new EncryptUtils().getMD5Str("minggo" + imei + "charmword");
			params.put(ApiUrl.pass, md5pass);
			params.put(ApiUrl.m_ver, URLEncoder.encode(m_ver, "utf-8"));
			params.put(ApiUrl.s_ver, URLEncoder.encode(s_ver, "utf-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return params;
	}

}
